package zombiedefense.util;

public class ObjectPool<T extends Poolable>{
	
	private Poolable[] pooledObjects;
	
	/**
	* Creates a new instance of an object pool of
	 * the specified size.
	*/
	public ObjectPool(Class<T> c,int numObjects){
	    pooledObjects = new Poolable[numObjects];
	    
	    try{
		for(int i = 0; i< pooledObjects.length; i++){
		    T obj = c.newInstance();
		    pooledObjects[i] = obj;
		}
	    }catch(Exception e){e.printStackTrace();}
	}
	
	/**
	* Returns the first pooled object that is not in use.
	*/
	public T getPooledObject(){
		for(Poolable object: pooledObjects){
			if(!object.isInUse()){
				object.reset();
				return (T)object;
			}
		}
		
		return null;
	}
	
	
}